/*
 * This file is part of aion-unique <aionunique.smfnew.com>.
 *
 *  aion-unique is free software: you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation, either version 3 of the License, or
 *  (at your option) any later version.
 *
 *  aion-unique is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with aion-unique.  If not, see <http://www.gnu.org/licenses/>.
 */
package com.aionemu.gameserver.network.aion.clientpackets;

import com.aionemu.gameserver.ai.state.AIState;
import com.aionemu.gameserver.dataholders.DataManager;
import com.aionemu.gameserver.model.gameobjects.AionObject;
import com.aionemu.gameserver.model.gameobjects.Creature;
import com.aionemu.gameserver.model.gameobjects.Letter;
import com.aionemu.gameserver.model.gameobjects.Npc;
import com.aionemu.gameserver.model.gameobjects.player.Player;
import com.aionemu.gameserver.network.aion.AionClientPacket;
import com.aionemu.gameserver.network.aion.serverpackets.SM_LOOKATOBJECT;
import com.aionemu.gameserver.network.aion.serverpackets.SM_MAIL_SERVICE;
import com.aionemu.gameserver.utils.PacketSendUtility;
import com.aionemu.gameserver.world.World;

import java.util.Collection;

public class CM_CLOSE_DIALOG extends AionClientPacket
{
	/**
	* Target object id that client wants to TALK WITH or 0 if wants to unselect
	*/
	private int	targetObjectId;

	/**
	* Constructs new instance of <tt>CM_CM_REQUEST_DIALOG </tt> packet
	* @param opcode
	*/
	public CM_CLOSE_DIALOG(int opcode)
	{
		super(opcode);
	}

	/**
	* {@inheritDoc}
	*/
	@Override
	protected void readImpl()
	{
		targetObjectId = readD();
	}

	/**
	* {@inheritDoc}
	*/
	@Override
	protected void runImpl()
	{
		AionObject targetObject = World.getInstance().findAionObject(targetObjectId);
		Player player = getConnection().getActivePlayer();

		if(targetObject == null || player == null)
			return;

		if(targetObject instanceof Npc)
		{
			Npc npc = (Npc) targetObject;
            // Zephyr Deliveryman
            if (targetObject.getObjectId() == player.getZephyrObjectId()) {
                int zid = npc.getObjectId();
                AionObject obj = World.getInstance().findAionObject(zid);
                if (obj != null && obj instanceof Creature) {
                    Creature zephyr = (Creature) obj;
                    DataManager.SPAWNS_DATA.removeSpawn(zephyr.getSpawn());
                    zephyr.getController().delete();
                }
                player.setZephyrObjectId(0);

                // Refresh Mails client display
                Collection<Letter> lts = player.getMailbox().getLetters();
                int mailCount = 0;
                int unreadMailCount = 0;
                boolean hasExpress = false;
                boolean isNewMail =false;
				boolean isUnread =false;

                for (Letter lt : lts) {

                    if (lt.isUnread()) {isUnread =true; isNewMail = true;

                        if (!hasExpress && lt.isExpress())
                            hasExpress = true;
                    }
                }

                PacketSendUtility.sendPacket(player, new SM_MAIL_SERVICE(isNewMail, isUnread, hasExpress));

                return;
            }
			//TODO: need check it on retail

			if(npc.hasWalkRoutes() && !npc.getMoveController().canWalk())//resumes npc behavior
				npc.getMoveController().setCanWalk(true);
			else
				npc.getAi().setAiState(AIState.THINKING);

			if(!((Npc)targetObject).getIsQuestBusy())
			{
				if(npc.getTarget() == player)
					npc.setTarget(null);
				PacketSendUtility.broadcastPacket((Npc) targetObject,
					new SM_LOOKATOBJECT((Npc) targetObject));
			}
		}
	}
}
